<?php

/**
 * 加载数据类
 * 
 * @package    01CMS
 * @subpackage system
 * @author     rolong at vip.qq.com
 * @version    1.0.0
 * @link       http://www.01cms.com
 */

class Load
{
    public $config = array();
    public $var = array();
    
    private $object = array();
    private $conf = array();
    private $data = array();
    private $error = '';
    private $Smarty;
    private $Base;
    private $templateName;

    function __construct ()
    {
        $this->config = $this->_file('config', 'config');
        
        $this->var = include_once (SYS_PATH . '/config/var.php');
        
        if (file_exists(APP_PATH . '/config/var.php'))
        {
            $var = include_once (APP_PATH . '/config/var.php');
            $this->var = array_merge($this->var, $var);
            unset($var);
        }
    }

    /**
     * 加载类库
     *
     * @param string $libName
     * @param mix $param
     * @return object
     */
    function lib ($libName, $param = NULL)
    {
        return $this->_class($libName, $param);
    }

    /**
     * 加载模型
     *
     * @param string $libName
     * @param mix $param
     * @return object
     */
    function model ($modelName, $param = NULL)
    {
        return $this->_class($modelName . 'Model', $param);
    }

    /**
     * 加载数据
     *
     * @param string $libName
     * @param mix $param
     * @return object
     */
    function data ($fileName)
    {
        return $this->_file($fileName, 'data');
    }

    /**
     * 加载配置文件
     *
     * @param string $libName
     * @param mix $param
     * @return object
     */
    function config ($fileName)
    {
        return $this->_file($fileName, 'config');
    }

    /**
     * 加载对象
     * 
     * @param string $fileName
     * @param string $param
     * @return object
     * @access public
     */
    public function &_class ($className, $param = NULL)
    {
        $paramId = (is_string($param) || is_int($param)) ? $param : '';
        $classId = $className . $paramId;
        
        if (isset($this->object[$classId]))
        {
            return $this->object[$classId];
        }
        
        $this->object[$classId] = new $className($param);
        
        return $this->object[$classId];
    }

    /**
     * 加载数据文件
     * 
     * @param string $fileName
     * @param string $type [data|config]
     * @return array
     * @access public
     */
    public function &_file ($fileName, $type = 'data')
    {
        if (strpos($fileName, '/') !== FALSE)
        {
            $fileNameArr = explode('/', $fileName);
            $varName = $type . ucfirst($fileNameArr[0]) . ucfirst($fileNameArr[1]);
        }
        else
        {
            $varName = $type . $fileName;
        }
        
        if (isset($this->data[$varName]))
        {
            return $this->data[$varName];
        }
        
        if (file_exists(APP_PATH . "/{$type}/" . $fileName . '.php'))
        {
            $this->data[$varName] = include_once (APP_PATH . "/{$type}/" . $fileName . '.php');
        }
        else if (file_exists(SYS_PATH . "/{$type}/" . $fileName . '.php'))
        {
            $this->data[$varName] = include_once (SYS_PATH . "/{$type}/" . $fileName . '.php');
        }
        else
        {
            return FALSE;
        }
        
        return $this->data[$varName];
    }

    /**
     * 加载Smarty
     * 
     * @param string $templateSuffix
     * @param int $lifeTime
     * @return void
     * @access public
     */
    public function smarty ($templateSuffix = '', $lifeTime = -1)
    {
        $this->Base = & Base::i();
        $this->templateName = $this->Base->m . ucfirst($templateSuffix) . '.tpl';
        $tplPath = isset($this->Base->templateDir) ? $this->Base->templateDir : APP_PATH . '/view/' . $this->Base->c;
        $this->Smarty = $this->_class('Smarty');
        //$Smarty->debugging = true;
        //$this->Smarty->config_dir = '';
        //$this->Smarty->use_sub_dirs = true;
        $this->Smarty->template_dir = $tplPath;
        $compile_dir = APP_PATH . '/data/compile/' . $this->Base->c . '/' . $this->Base->m;
        
        if (! file_exists($compile_dir))
        {
            makeDirs($compile_dir);
        }
        
        if (empty($lifeTime))
        {
            $caching = FALSE;
        }
        else
        {
            $caching = 1;
            
            $cache_dir = APP_PATH . '/data/smarty/cache/' . $this->Base->c . '/' . $this->Base->m;
            
            if (! file_exists($cache_dir))
            {
                makeDirs($cache_dir);
            }
            
            $this->Smarty->cache_dir = $cache_dir;
        }
        
        $this->Smarty->compile_dir = $compile_dir;
        $this->Smarty->compile_id = isset($this->Base->compileId) ? $this->Base->compileId : '';
        $this->Smarty->plugins_dir = array(APP_PATH . '/plugin', SYS_PATH . '/plugin', SYS_PATH . '/lib/Smarty/plugins');
        $this->Smarty->caching = $caching;
        
        if ($caching)
        {
            $this->Smarty->cache_lifetime = $lifeTime;
            if ($this->Smarty->is_cached($this->templateName))
            {
                $this->Smarty->display($this->templateName);
                exit();
            }
        }
    }

    /**
     * 加载视图
     * 
     * @param string $type
     * @param string $templateSuffix
     * @return void
     * @access public
     */
    public function view ($type = '', $templateSuffix = '')
    {
        if ('smarty' == strtolower($type))
        {
            if (! is_object($this->Smarty))
            {
                $this->smarty($templateSuffix, 0);
            }
            
            $this->Smarty->left_delimiter = '{:';
            $this->Smarty->assign_by_ref('i', Base::i());
            $this->Smarty->display($this->templateName);
        }
        else if ('php' == strtolower($type) || empty($type))
        {
            global $c, $m;
            $tplPath = APP_PATH . '/view/' . $c;
            if (! file_exists($tplPath . '/' . $m . ucfirst($templateSuffix) . '.tpl'))
            {
                trigger_error('模板文件 ' . $tplPath . '/' . $m . ucfirst($templateSuffix) . '.tpl 不存在', E_USER_ERROR);
            }
            
            include ($tplPath . '/' . $m . ucfirst($templateSuffix) . '.tpl');
        }
    }

    /**
     * 获取POST变量
     * 
     * @param string $keys
     * @param string $type
     * @return mix
     * @access public
     */
    public function post ($keys, $type = '')
    {
        $data = $this->processData($_POST, $keys);
        return ($type == 'one') ? $data[$keys] : $data;
    }

    /**
     * 获取GET变量
     * 
     * @param string $keys
     * @param string $type
     * @return mix
     * @access public
     */
    public function get ($keys, $type = '')
    {
        $data = $this->processData($_GET, $keys);
        return ($type == 'one') ? $data[$keys] : $data;
    }

    /**
     * 获取查询变量
     * 
     * @param string $varString
     * @param string $type
     * @return mix
     * @access public
     */
    public function query ($varString, $type = '')
    {
        if (! empty($_SERVER['QUERY_STRING']))
        {
            parse_str($_SERVER['QUERY_STRING']);
            $varNames = explode(',', $varString);
            $data = array();
            foreach ($varNames as $varName)
            {
                $data[$varName] = isset($$varName) ? slashQuotes($$varName, 'add', TRUE) : '';
            }
            
            return ($type == 'one') ? $data[$varString] : $data;
        }
        
        return FALSE;
    }

    /**
     * 获取错误信息
     * 
     * @return string
     * @access public
     */
    public function getError ()
    {
        return $this->error;
    }

    private function processData ($array, $keys)
    {
        $keys = explode(',', $keys);
        $return = array();
        foreach ($keys as $key)
        {
            $key = explode('|', $key);
            $field = $key[0];
            if (isset($array[$field]))
            {
                $curValue = slashQuotes($array[$field]);
                if (is_array($curValue))
                {
                    foreach ($curValue as $subCurValue)
                    {
                        if (empty($subCurValue))
                        {
                            $this->error = $key[1];
                            return FALSE;
                        }
                    }
                }
                else if (isset($key[1]) && empty($curValue))
                {
                    $this->error = $key[1];
                    return FALSE;
                }
                $return[$field] = $curValue;
            }
            else
            {
                if (isset($key[1]))
                {
                    $this->error = $key[1];
                    return FALSE;
                }
                
                $return[$field] = NULL;
            }
        }
        return $return;
    }
}

// END